package com.boc.langxiang.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.boc.langxiang.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * CreateTime: 2016-06-08下午2:45
 * Author: wjkjinke00@126.com
 * Description:
 */
public class CustomAlertDialog extends DialogFragment implements View.OnClickListener {

    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.message)
    TextView mMessage;
    @BindView(R.id.negative)
    TextView mNegative;
    @BindView(R.id.positive)
    TextView mPositive;
    @BindView(R.id.view_btn_space)
    View mViewBtnSpace;
    @BindView(R.id.button_layout)
    LinearLayout mButtonLayout;

    private Builder mBuilder;

    /**
     * @return
     */
    public static CustomAlertDialog newInstance(Builder builder) {
        CustomAlertDialog dialog = new CustomAlertDialog();
        dialog.mBuilder = builder;
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        if (dialog == null) {
            return null;
        }
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        View view = inflater.inflate(R.layout.dialog_custom, container);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (TextUtils.isEmpty(mBuilder.title)) {
            mTitle.setVisibility(View.GONE);
        } else {
            mTitle.setText(mBuilder.title);
        }

        if (TextUtils.isEmpty(mBuilder.message)) {
            mMessage.setVisibility(View.GONE);
        } else {
            mMessage.setText(mBuilder.message);
        }

        if (TextUtils.isEmpty(mBuilder.negative) && TextUtils.isEmpty(mBuilder.positive)) {
            mButtonLayout.setVisibility(View.GONE);
        } else {
            if (TextUtils.isEmpty(mBuilder.negative) || TextUtils.isEmpty(mBuilder.positive)) {
                mViewBtnSpace.setVisibility(View.GONE);
            }

            if (TextUtils.isEmpty(mBuilder.negative)) {
                mNegative.setVisibility(View.GONE);
            } else {
                mNegative.setText(mBuilder.negative);
            }

            if (TextUtils.isEmpty(mBuilder.positive)) {
                mPositive.setVisibility(View.GONE);
            } else {
                mPositive.setText(mBuilder.positive);
            }

        }

        mNegative.setOnClickListener(this);
        mPositive.setOnClickListener(this);

    }

    public void show(FragmentManager fragmentManager) {
        show(fragmentManager, CustomAlertDialog.class.getSimpleName());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        if (mBuilder.listener != null) {
            if (v == mNegative) {
                mBuilder.listener.onNegativeButtonClick();
                dismissAllowingStateLoss();
            } else if (v == mPositive) {
                mBuilder.listener.onPositiveButtonClick();
                dismissAllowingStateLoss();
            }
        }

    }

    public interface CustomAlertDialogClickListener {
        void onPositiveButtonClick();

        void onNegativeButtonClick();
    }

    public static class Builder {
        private String title;
        private String message;
        private String negative;
        private String positive;
        private Context mContext;

        public Builder(Context context) {
            mContext = context;
        }

        private CustomAlertDialogClickListener listener;

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setTitle(int title) {
            this.title = mContext.getString(title);
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setMessage(int message) {
            this.message = mContext.getString(message);
            return this;
        }

        public Builder setNegative(String negative) {
            this.negative = negative;
            return this;
        }

        public Builder setNegative(int negative) {
            this.negative = mContext.getString(negative);
            return this;
        }

        public Builder setPositive(String positive) {
            this.positive = positive;
            return this;
        }

        public Builder setPositive(int positive) {
            this.positive = mContext.getString(positive);
            return this;
        }

        public Builder setListener(CustomAlertDialogClickListener listener) {
            this.listener = listener;
            return this;
        }
    }

}
